/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.lidovky.articles.importLogRestore;

import cz.mzk.kramerius.fedoraaccess.resourceIndex.ResourceIndexReader;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class ImportLogBuilder {

    private final ResourceIndexReader reader;

    public ImportLogBuilder(String resourceIndexUrl) {
        reader = new ResourceIndexReader(resourceIndexUrl);
    }

    public Document createImportLog(String articlePid) {
        List<String> issues = reader.getSubjects(articlePid, "hasIntCompPart");
        return buildImportLog(articlePid, issues);
    }

    private Document buildImportLog(String articlePid, List<String> issues) {
        Document doc = DocumentHelper.createDocument();
        Element article = doc.addElement("article");
        article.addAttribute("PID", articlePid);
        for (String issuePid : issues) {
            Element issue = article.addElement("issue");
            issue.addAttribute("PID", issuePid);
        }
        return doc;
    }
}
